﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Per_pixel_OCR
{
    public class Pixel
    {
        public double[] ClassIDHistogram { get; set; }

        public byte X { get; private set; }
        public byte Y { get; private set; }
        
        public byte ClassID
        {
            get
            {
                return this.Owner.TagMatrix[this.Y, this.X];    
            }
            set
            {
                this.Owner.TagMatrix[Y, X] = value;
            }
        }

        public Digit Owner { get; private set; }

        public Pixel(Digit owner, byte x, byte y)
        {
            this.X = x;
            this.Y = y;
            this.Owner = owner;
        }

        public int GetNeighbor(int offsetX, int offsetY)
        {
            int rVal = 0;

            int newX = offsetX + this.X;
            int newY = offsetY + this.Y;

            //if (!IsCoordinatesInImage(newX, newY))
            //    return 0;
            
            WrapCoordinates(ref newX, ref newY);

            rVal = (int) this.Owner.OriginalGrayscaleMatrix[newY, newY];

            return rVal;
        }

        public bool ComputeFeature(Feature f)
        {
            bool rVal = false;

            int temp = 0;

            if (f.FeatureType == FeatureType.MINUS)
                temp = GetNeighbor(f.Offset1X, f.Offset1Y) - GetNeighbor(f.Offset2X, f.Offset2Y);
            else if (f.FeatureType == FeatureType.PLUS)
                temp = GetNeighbor(f.Offset1X, f.Offset1Y) + GetNeighbor(f.Offset2X, f.Offset2Y);

            if (temp < f.Threshold)
                rVal = false;
            else
                rVal = true;

            return rVal;
        }

        private bool IsCoordinatesInImage(int x, int y)
        {
            if (x > -1 && y > -1 && x < this.Owner.OriginalGrayscaleMatrix.GetLength(1) && y < this.Owner.OriginalGrayscaleMatrix.GetLength(0))
                return true;
            return false;
        }

        private void WrapCoordinates(ref int x, ref int y)
        {
            if (x < 0)
            {
                x = x + this.Owner.OriginalGrayscaleMatrix.GetLength(1);
            }

            if (x >= this.Owner.OriginalGrayscaleMatrix.GetLength(1))
            {
                x = x - this.Owner.OriginalGrayscaleMatrix.GetLength(1);
            }
            if (y < 0)
            {
                y = y + this.Owner.OriginalGrayscaleMatrix.GetLength(0);
            }

            if (y >= this.Owner.OriginalGrayscaleMatrix.GetLength(0))
            {
                y = y - this.Owner.OriginalGrayscaleMatrix.GetLength(0);
            }
        }

        public override string ToString()
        {
            return "Digit: " + this.Owner.DigitValue.ToString() + " - ClassID: " + this.ClassID.ToString() + " - X,Y: " + this.X.ToString() + ", " + this.Y.ToString();
        }
    }
}
